﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow
{
    public partial class File : System.Web.UI.Page
    {
        protected string IndexUrl = "/Index.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(Session["isadmin"]) == false)
            {
                IndexUrl = "ForecasterIndex.aspx";
            }
            //管理员操作按钮组
            if (Convert.ToBoolean(Session["isadmin"]) == false)
            {
                btn_add_.Visible = false;
                btn_check_all.Visible = false;
                btn_deleete_checked.Visible = false;

            }

            this.anp.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"]);
            if (!IsPostBack)
            {
                Search("");
            }
        }

        #region 全选
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_check_all_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow item in gv_result.Rows)
            {
                (item.FindControl("Select") as CheckBox).Checked = true;
            }
        } 
        #endregion

        #region 删除选中
        /// <summary>
        /// 删除选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_deleete_checked_Click(object sender, EventArgs e)
        {
            CheckBox cb = null;
            Label lbl_id = null;
            string sql = "delete from files where id=";
            codeDAL.SqlHelper sqlhelper = new codeDAL.SqlHelper();
            foreach (GridViewRow item in gv_result.Rows)
            {
                cb = item.FindControl("Select") as CheckBox;
                if (cb.Checked)
                {
                    lbl_id = item.FindControl("ID") as Label;
                    string searchsql = "select downloadpath from files where id=" + lbl_id.Text.Trim();
                    string filename = sqlhelper.ExecuteScalar(searchsql).ToString();
                    string exesql = sql + lbl_id.Text.Trim();
                    sqlhelper.ExecuteNonQuery(exesql, null);
                    if (System.IO.File.Exists(Path.Combine(Server.MapPath("uploadfiles"), filename)))
                    {
                        System.IO.File.Delete(Path.Combine(Server.MapPath("uploadfiles"), filename));
                    }
                }
            }
            if (anp.CurrentPageIndex == anp.PageCount)
            {
                anp.CurrentPageIndex = 1;
            }
            Search("");
        } 
        #endregion

        #region 条件搜索方法
        /// <summary>
        /// 条件搜索方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_search_Click(object sender, EventArgs e)
        {
            if (cbx_by_month.Checked)
            {
                Search(txt_filename.Text.Trim());
            }
            else
            {
                Search("");
            }
        } 
        #endregion

        #region 获得完整文件名
        /// <summary>
        /// 获得完整文件名
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        protected string EchoFileName(string input)
        {
            return Path.Combine("uploadfiles", input);
        } 
        #endregion

        #region 条件搜索
        /// <summary>
        /// 条件搜索
        /// </summary>
        /// <param name="filter"></param>
        private void Search(string filter)
        {
            string sql = "", condition = "";
            if (string.IsNullOrEmpty(filter))
            {
                sql = string.Format("select * from files");
            }
            else
            {
                sql = string.Format("select * from files where [filename] like '%{0}%'", filter);
                condition = "[filename] like '%" + filter + "%'";
            }
            int pagesize = anp.PageSize;
            int pageindex = anp.CurrentPageIndex;
            anp.RecordCount = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text).Rows.Count;
            gv_result.DataSource = new codeDAL.Common().Search("files", pagesize, pageindex, condition, "*");
            gv_result.DataBind();
            if (anp.RecordCount == 0)
            {
                gv_result.Visible = false;
                anp.Visible = false;
                div_nodata.Visible = true;
            }
            else
            {
                gv_result.Visible = true;
                anp.Visible = true;
                div_nodata.Visible = false;
            }
        } 
        #endregion

        #region 上传文件
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (file.HasFile)
            {
                string userFileName = txt_file_name.Text.Trim();

                string filename = file.FileName;
                string extention = Path.GetExtension(filename);
                if (!string.IsNullOrEmpty(userFileName))
                {
                    filename = userFileName;
                }
                string guid = Guid.NewGuid().ToString();
                
                string path = Path.Combine(Server.MapPath(@"uploadfiles"), guid) + extention;
                file.SaveAs(path);
                string sql = string.Format("insert into files values('{0}','{1}','{2}','{3}')", filename, path, guid, guid + extention);
                new codeDAL.SqlHelper().ExecuteNonQuery(sql, null);
                Common.RegisterScriptAtPageLast(Page, "alert('上传成功')");
                txt_file_name.Text = "";
            }
            else
            {
                Common.RegisterScriptAtPageLast(Page, "alert('没有选择文件')");
            }
            cbx_by_month.Checked = false;
            Search("");
        } 
        #endregion

        #region 翻页
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            if (cbx_by_month.Checked)
            {
                Search(txt_filename.Text.Trim());
            }
            else
            {
                Search("");
            }
        } 
        #endregion
    }
}